package com.wanban.adutils.advertisers.ironsource

import android.app.Activity
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.ironsource.mediationsdk.ads.nativead.LevelPlayMediaView
import com.ironsource.mediationsdk.ads.nativead.LevelPlayNativeAd
import com.ironsource.mediationsdk.ads.nativead.LevelPlayNativeAdListener
import com.ironsource.mediationsdk.ads.nativead.NativeAdLayout
import com.ironsource.mediationsdk.ads.nativead.interfaces.NativeAdDataInterface
import com.ironsource.mediationsdk.logger.IronSourceError
import com.wanban.adutils.R
import com.wanban.adutils.advertisers.BaseAdLoader
import com.wanban.adutils.callback.OnLoadAdCallback
import com.wanban.adutils.db.bean.AdInfo
import com.wanban.adutils.tools.AdConstants


class ISNativeAdUtils : BaseAdLoader() {

    private var nativeAd: LevelPlayNativeAd? = null

    override fun loadAdNew(activity: Activity, adInfo: AdInfo, callback: OnLoadAdCallback) {
        super.loadAdNew(activity, adInfo, callback)
//        native = IronSource.createBanner(activity, ISBannerSize.BANNER)
//        native?.levelPlayBannerListener = object: LevelPlayBannerListener {
//            override fun onAdLoaded(p0: com.ironsource.mediationsdk.adunit.adapter.utility.AdInfo?) {
//                activity.runOnUiThread {
//                    if (isTimeout) {
//                        destroy()
//                        return@runOnUiThread
//                    }
//                    setAdLoadSuccess()
//                    sdkLoadFinish()
//                    mOnLoadAdCallback?.onAdLoaded(native)
//                }
//            }
//
//            override fun onAdLoadFailed(error: IronSourceError?) {
//                activity.runOnUiThread {
//                    if (isTimeout) {
//                        destroy()
//                        return@runOnUiThread
//                    }
//                    sdkLoadFinish()
//                    val code = error?.errorCode?:-9999
//                    analysisErrorCode(adInfo, code)
//                    mOnLoadAdCallback?.onAdLoadFailed(code)
//                    destroy()
//                }
//            }
//
//            override fun onAdClicked(p0: com.ironsource.mediationsdk.adunit.adapter.utility.AdInfo?) {
//                activity.runOnUiThread {
//                    mOnLoadAdCallback?.onAdClicked()
//                }
//            }
//
//            override fun onAdLeftApplication(p0: com.ironsource.mediationsdk.adunit.adapter.utility.AdInfo?) {
//            }
//
//            override fun onAdScreenPresented(p0: com.ironsource.mediationsdk.adunit.adapter.utility.AdInfo?) {
//                activity.runOnUiThread {
//                    setAdLoadShowing(true)
//                    mOnLoadAdCallback?.onAdOpened()
//                }
//            }
//
//            override fun onAdScreenDismissed(p0: com.ironsource.mediationsdk.adunit.adapter.utility.AdInfo?) {
//                activity.runOnUiThread {
//                    setAdLoadShowing(false)
//                    mOnLoadAdCallback?.onAdComplete()
//                }
//            }
//        }
//        IronSource.loadBanner(native)


        nativeAd = LevelPlayNativeAd.Builder()
            .withPlacementName(realUnitId)
            .withListener(object: LevelPlayNativeAdListener {
                override fun onAdClicked(nativeAd: LevelPlayNativeAd?, adInfo: com.ironsource.mediationsdk.adunit.adapter.utility.AdInfo?) {
                    activity.runOnUiThread {
                        mOnLoadAdCallback?.onAdClicked()
                    }
                }

                override fun onAdImpression(nativeAd: LevelPlayNativeAd?, adInfo: com.ironsource.mediationsdk.adunit.adapter.utility.AdInfo?) {
                    activity.runOnUiThread {
                        setAdLoadShowing(true)
                        mOnLoadAdCallback?.onAdOpened()
                    }
                }

                override fun onAdLoadFailed(nativeAd: LevelPlayNativeAd?, error: IronSourceError?) {
                    activity.runOnUiThread {
                        if (isTimeout) {
                            destroy()
                            return@runOnUiThread
                        }
                        sdkLoadFinish()
                        val code = error?.errorCode?:-9999
                        analysisErrorCode(adInfo, code)
                        mOnLoadAdCallback?.onAdLoadFailed(code)
                        destroy()
                    }
                }

                override fun onAdLoaded(nativeAd: LevelPlayNativeAd?, adInfo: com.ironsource.mediationsdk.adunit.adapter.utility.AdInfo?) {
                    activity.runOnUiThread {
                        if (isTimeout) {
                            destroy()
                            return@runOnUiThread
                        }
                        setAdLoadSuccess()
                        sdkLoadFinish()
                        mOnLoadAdCallback?.onAdLoaded(nativeAd)
                    }
                }

            })
            .build()

        // You can add the native ad object to a list of native ads (mNativeAds), which can be used to display the ad when needed
        nativeAd?.loadAd()
    }


    override fun showAd(adRoot: ViewGroup, type: Int) {
        if (nativeAd == null) {
            mOnLoadAdCallback?.onAdLoadFailed(AdConstants.ERROR_CODE_INVALID_REQUEST)
            destroy()
            return
        }

        val inflater = adRoot.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater?
        val nativeAdLayout: NativeAdLayout = inflater?.inflate(R.layout.is_native, null) as NativeAdLayout

        // Locate all views to be populated
        val titleView = nativeAdLayout.findViewById<TextView>(R.id.ad_title)
        val iconView = nativeAdLayout.findViewById<ImageView>(R.id.ad_app_icon)
        val mediaView = nativeAdLayout.findViewById<LevelPlayMediaView>(R.id.ad_media)
        val call2ActionView = nativeAdLayout.findViewById<TextView>(R.id.is_ad_call_to_action)
        // Populate the views
        titleView.text = nativeAd?.title
        val icon: NativeAdDataInterface.Image? = nativeAd?.icon
        if (icon != null) {
            if (icon.drawable != null) {
                iconView.setImageDrawable(icon.drawable)
            }
        }

        // Register the views to be bound
        nativeAdLayout.setTitleView(titleView)
        nativeAdLayout.setIconView(iconView)
        nativeAdLayout.setMediaView(mediaView)
        nativeAdLayout.setCallToActionView(call2ActionView)

        // Setting the native ads
        if (nativeAd != null) {
            nativeAdLayout.registerNativeAdViews(nativeAd!!)
        }


        adRoot.removeAllViews()
        adRoot.visibility = View.VISIBLE
        val layoutParams = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT
        )
        layoutParams.gravity = Gravity.CENTER
        when (type) {
            AdConstants.ADTYPE_OPEN -> {
                adRoot.addView(nativeAdLayout, 0, layoutParams)
                countDownOpenAd(adRoot)
            }

            AdConstants.ADTYPE_PAUSE_VIDEO -> {
                adRoot.addView(nativeAdLayout, 0, layoutParams)
            }

            AdConstants.ADTYPE_INSTREAM_VIDEO -> {
                adRoot.addView(nativeAdLayout, 0, layoutParams)
                countDownInstreamAd(adRoot)
            }

            else -> {
                mOnLoadAdCallback?.onAdLoadFailed(AdConstants.ERROR_CODE_AD_LOADED_CAN_NOT_SHOW)
                destroy()
            }
        }
    }


    override fun destroy() {
        super.destroy()
        if (nativeAd == null) return
        nativeAd?.destroyAd()
    }

}